/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf.service;

import com.jsf.service.Car;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.inject.Named;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Admin
 */
@Named
public class LazyModel extends LazyDataModel<Car>{
    
    private List<Car> datasource;
     
    public LazyModel(List<Car> datasource) {
        this.datasource = datasource;
    }
     
    @Override
    public Car getRowData(String rowKey) {
        for(Car car : datasource) {
            if(car.getRandomId().equals(rowKey))
                return car;
        }
 
        return null;
    }
 
    @Override
    public Object getRowKey(Car car) {
        return car.getRandomId();
    }
 
    @Override
    public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
        List<Car> data = new ArrayList<Car>();
 
        //filter
        for(Car car : datasource) {
            boolean match = true;
 
            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        System.out.println("com.mycompany.service.LazyModel.load()=======fieldValue");
                        String filterProperty = it.next();
                        String filterValue = String.valueOf(filters.get(filterProperty));
                        //这里一直存在问题
                        //获取该字段的对象,但是一直在这里死循环
                        String filedValue ;
                        if("randomBrand".equals(filterProperty)){
                            filedValue = car.getRandomBrand();
                        }else if("randomColor".equals(filterProperty)){
                            filedValue = car.getRandomColor();
                        }else{
                             filedValue = car.getRandomId();
                        }
                       
//                        String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car));
                        System.out.println("com.mycompany.service.LazyModel.load()"+filedValue);
                        if(filterValue == null || filedValue.startsWith(filterValue.toString())) {
                            match = true;
                    }
                    else {
                            match = false;
                            break;
                        }
                    } catch(Exception e) {
                        match = false;
                    }
                }
            }
 
            if(match) {
                data.add(car);
            }
        }
 
        //sort
        if(sortField != null) {
//            Collections.sort(data, new LazySorter(sortField, sortOrder));
        }
 
        //rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);
 
        //paginate
        if(dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return data;
        }
    }
    
    
}
